﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedeSocial.DataAccess;

namespace RedeSocial.ViewModels
{
	public class Resource
	{
		public Resource()
		{
			Resources = new SortedDictionary<string, string>();
		}
		#region fields...
		private int _languageId = 1;
		#endregion
		#region properties...
		private SortedDictionary<string, string> Resources { get; set; }
		/// <summary>
		/// Gets or sets the language id.
		/// </summary>
		/// <value>
		/// The language used by the resource manager.
		/// </value>
		/// <remarks>
		/// When the language changes any resources that have been previous loaded will be discarded.
		/// </remarks>
		public int LanguageId
		{
			get { return _languageId; }
			set
			{
				if (_languageId != value)
				{
					_languageId = value;
					Resources.Clear();
				}
			}
		}
		#endregion
		#region methods...
		/// <summary>
		/// Gets the resource string for the given code.
		/// </summary>
		/// <param name="code">The code.</param>
		/// <returns></returns>
		public string GetResource(string code)
		{
			string retVal;
			if (Resources.ContainsKey(code))
				retVal = Resources[code];
			else
			{
				using (RedeSocialEntities dataContext = new RedeSocialEntities())
				{
					retVal = dataContext.ResourceLanguages.Where(rs => rs.Language.Id == LanguageId && rs.Resource.Code.Equals(code, StringComparison.CurrentCultureIgnoreCase)).Select(rs => rs.Value).FirstOrDefault();
					if (string.IsNullOrEmpty(retVal))
						retVal = "N/A";
					Resources.Add(code, retVal);
				}
			}
			return retVal;
		}
		#endregion
	}
}
